package com.lenovo.smarttraffic.ui.fragment;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.MetroDialogEventBusBean;
import com.lenovo.smarttraffic.ui.adapter.MyDialogRecyclerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Caleb.F
 * on 2019-11-26
 * at 14:32
 */
public class MetroRecyclerFragment extends DialogFragment {
    private TextView mTvMetroLine;
    private RecyclerView mDialogRecycler;
    private String LocationType;
    private  onLocationSelectListener mOnLocationSelectListener;

    private static final String TAG = "MetroRecyclerFragment";

    public MetroRecyclerFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        LocationType=  getArguments().getString(Constant.METRO_LOCATION_TYPE);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //window等设置
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        final Window window = getDialog().getWindow();
        View inflate =inflater.inflate(R.layout.dialog_recycler_view, container, false);
        window.setBackgroundDrawableResource(R.drawable.recycler_background);
        window.requestFeature(Window.FEATURE_NO_TITLE);
        return inflate;
    }


    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getMetroRecyclerInfo(MetroDialogEventBusBean metroDialogEventBusBean) {
        mTvMetroLine.setText(metroDialogEventBusBean.getMetroInfoBean().getROWS_DETAIL().get(0).getName());
        mDialogRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        MyDialogRecyclerAdapter myDialogRecyclerAdapter = new MyDialogRecyclerAdapter(getActivity(),metroDialogEventBusBean);
        mDialogRecycler.setAdapter(myDialogRecyclerAdapter);
        myDialogRecyclerAdapter.setRecyclerItemListener(s->{
           if(LocationType.equals(Constant.METRO_FIRST_LOCATION)){
               mOnLocationSelectListener.onFirstLocationSelect(s);
           }
           else if(LocationType.equals(Constant.METRO_LAST_LOCATION)){
               mOnLocationSelectListener.onLastLocationSelect(s);
           }
        });
    }



    private void initView(View convertView) {
        mTvMetroLine = convertView.findViewById(R.id.tv_metro_line);
        mDialogRecycler = convertView.findViewById(R.id.dialog_recycler);

    }

    public  interface  onLocationSelectListener{
        void onFirstLocationSelect(String s);
        void onLastLocationSelect(String s);
    }
    public void serOnLocationListener(onLocationSelectListener onLocationSelectListener){
        mOnLocationSelectListener=onLocationSelectListener;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
